package
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.SizeManager;
	import assists.URI;
	import assists.log.Log;
	import assists.resource.ResourceLoader;
	
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.TransformGestureEvent;
	import flash.system.Capabilities;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	import main.ClientConstants;
	import main.DataManager;
	import main.LocalStoreInfo;
	import main.ViewManager;
	import main.XMLLoaderManager;
	
	import views.ViewLayout;
	import com.clouree.anelib.AneEventsDispatcher;
	
	[SWF(backgroundColor='0xeeeeee')]
	public class App extends Sprite
	{
		private var _resourceLoader:ResourceLoader;//资源管理器
		private var _uri:URI;//路径拼接类
		private var _viewManager:ViewManager;//视图管理器
		private var _dataManager:DataManager;//数据管理器
		private var layout:ViewLayout;
		private var _lang:Lang;
		
		public function App()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.DEACTIVATE, deactivate);
			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			
			//			日志类
			initLog();
			_uri = new URI;
			if(CONFIG::ANDROID)
			{
				changeFontSize();
								var ane:AneEventsDispatcher = new AneEventsDispatcher();
								ane.updateAuto();
				//				ane.sunTime(37.4995,122.1819,8);//37.4995,122.1819
//												ane.showDialog("从本地应用调用");
				//				ane.setMessageNotifyType(false);
				//				ane.setMessageNotifyContent("这里是通知--------hello world","通知标题");
				//				ane.sendMessage("hello i am ane","13287870801");
				//				ane.callPhone("13287870801");
				
			}
			SizeManager.init(stage);
			_resourceLoader = new ResourceLoader();
			layout = new ViewLayout(stage);
			this.addChild(layout);
			
			var xmlLoaderManager:XMLLoaderManager = new XMLLoaderManager(_uri);
			
			_dataManager = new DataManager();
			_dataManager.init(xmlLoaderManager,_uri);
			
			_dataManager.cityList.loadList();
			
			_lang = new Lang(xmlLoaderManager.langXml);
			
			_lang.initThis(initView);
			
			var localInfo:LocalStoreInfo = new LocalStoreInfo();
			if(localInfo.readInfo("loadPic") == null)
			{
				ClientConstants.NET_MODE = ClientConstants.PIC_LOAD_YES;
			}else
			{
				ClientConstants.NET_MODE = localInfo.readInfo("loadPic").value;
			}
			if(localInfo.readInfo("userInfo") == null)
			{
			ClientConstants.USER_NAME = "游客" + uint(Math.random() * 10000000);
			localInfo.storeInfo("userInfo",{name:ClientConstants.USER_NAME});		
			}else
			{
				ClientConstants.USER_NAME = localInfo.readInfo("userInfo").name;
			}
		}
		
		private function initView():void
		{	
			_viewManager = new ViewManager();
			_viewManager.init(_dataManager,layout,_lang,_resourceLoader,_uri);
			
			layout.closeFunction = closeApp;
			
			/*if (Multitouch.supportsGestureEvents) {                                                                              //此处注释放开即开启log限制
			Multitouch.inputMode = MultitouchInputMode.GESTURE;
			stage.addEventListener(TransformGestureEvent.GESTURE_ROTATE,initLogHandler);
			}*/
			
			//			性能分析模块
			if(CONFIG::DebugMode)
			{
				//				MT.init(this,1);
			}
			
			initCityInfo();
			
			showPanel();
		}
		
		private function initCityInfo():void
		{
			var store:LocalStoreInfo = new LocalStoreInfo;
			var obj:Object = store.readInfo("choosenCity");
			if(obj == null){
				_dataManager.cityList.chooseCityId = "0631";
			}else{
				_dataManager.cityList.chooseCityId = String(obj);
			}
		}
		
		private function showPanel():void
		{
			_viewManager.background.show();
			_viewManager.guide.show();
			_viewManager.mainPanel.show();
			
			_dataManager.update.getUpdateList(ClientConstants.VERSION_NUM);
			
			if(_dataManager.cityList.chooseCity == ""){
				_viewManager.alert.showAlert("感谢使用本应用，请点击前往选择目标城市，同时建议下载离线数据包以节省流量。","确认","",function():void {_viewManager.downloadZip.show();});				
			}
		}
		
		private function changeFontSize():void
		{
			if(!Multitouch.supportsTouchEvents) return;
			var dpi:uint = flash.system.Capabilities.screenDPI;
			SizeManager.DPI = dpi;
			if(dpi < 200)
			{
				HtmlText.NUMBER_FONT_CHANGE = -2;
				_uri.dpiSize = 0;
			}else if(dpi >= 200 && dpi < 280)
			{
				HtmlText.NUMBER_FONT_CHANGE = 8;
				_uri.dpiSize = 1;
			}else if(dpi >= 280)
			{
				if(this.stage.stageWidth > 640){
					HtmlText.NUMBER_FONT_CHANGE = 16;
				}else{
					HtmlText.NUMBER_FONT_CHANGE = 10;
				}
				_uri.dpiSize = 2;
			}
		}
		
		protected function initLogHandler(event:TransformGestureEvent):void
		{
			if(event.rotation < -15){
				stage.removeEventListener(TransformGestureEvent.GESTURE_ROTATE,initLogHandler);
				initLog();
			}
		}
		
		private function initLog():void
		{
			var loggers:Object = new Object;
			loggers = {
				loggers: [
					{
						name: "ConsoleLogger",
						levels: "*",
						params:{ stage: this.stage }
					}
				]
			};
			
			Log.instance.init(loggers);
		}
		
		private function deactivate(e:Event):void 
		{
			//			NativeApplication.nativeApplication.exit();
		}
		
		private function closeApp():void
		{
			_viewManager.alert.showAlert("确认退出吗？","确认","取消",closeThis,cancel);
			
			function closeThis():void
			{
				NativeApplication.nativeApplication.exit();
			}
			
			function cancel():void
			{
				_viewManager.alert.close();
				_viewManager.alert.clear();
			}
		}
	}
}